﻿using CL.Business.Entity;
using CL.Dapper.Repository;
using System.Data;
using CL.Enum.Common;
using Dapper;
using System.Linq;
using System;
using System.Collections.Generic;
using CL.View.Entity.Business;
using System.Text;

namespace CL.Business.DAL
{
    public class AgencyUserDAL : DataRepositoryBase<AgencyUserEntity>
    {
        public AgencyUserDAL(DbConnectionEnum conenum, IDbConnection Db = null) : base(conenum, Db)
        {
        }



        /// <summary>
        /// 插入记录
        /// </summary>
        /// <param name="Entity"></param>
        /// <returns></returns>
        public int InsertEntity(AgencyUserEntity Entity)
        {
            return base.Insert(Entity) ?? 0;
        }


        /// <summary>
        /// 获取代理推广的会员列表
        /// </summary>
        /// <returns></returns>
        public List<AgencyUserEntity> QueryEntitys(int AccountID)
        {
            return base.GetList(new { AccountID = AccountID }, " AgencyID desc").ToList();
        }




        /// <summary>
        ///   推广明细
        /// </summary>
        /// <param name="AccountID">代理账号标识</param>
        /// <param name="PageIndex">页码</param>
        /// <param name="PageSize">页大小</param>
        /// <param name="RecordCount">返回行数</param>
        /// <returns></returns>
        public List<udv_PopularizeDetail> QueryExtendDetails(int AccountID, string StartTime,string EndTime,int PageIndex, int PageSize, string searchKey, ref int RecordCount)
        {
            var Parms = new DynamicParameters();
            Parms.Add("@AccountID", AccountID);
            Parms.Add("@StartTime", StartTime);
            Parms.Add("@EndTime", EndTime);
            Parms.Add("@SearchKey", searchKey);
            Parms.Add("@PageIndex", PageIndex);
            Parms.Add("@PageSize", PageSize);
            Parms.Add("@RecordCount", RecordCount, DbType.Int32, ParameterDirection.Output);
            var Entitys = new DataRepositoryBase<udv_PopularizeDetail>(DbConnectionEnum.CaileBusiness).QueryList("udp_PopularizeDetail", Parms, CommandType.StoredProcedure).ToList();
            RecordCount = Parms.Get<int>("@RecordCount");
            return Entitys;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="StartTime">开始时间</param>
        /// <param name="EndTime">结束时间</param>
        /// <param name="PageIndex">页码</param>
        /// <param name="PageSize">页大小</param>
        /// <param name="RecordCount">返回行数</param>
        /// <returns></returns>
        public List<udv_PopularizeDetail> SelExtendDetails(string StartTime, string EndTime, int PageIndex, int PageSize, ref int RecordCount)
        {
            var parms = new DynamicParameters();
            StringBuilder strSql = new StringBuilder();
            StringBuilder strTotalSql = new StringBuilder();
            strTotalSql.Append("SELECT  a.AccountID, a.AccountCode, b.UserID, c.OrderNo, c.PayType, c.Amount, CompleteTime, c.RechargeNo, c.OutRechargeNo, c.result FROM dbo.UN_Account a  INNER JOIN dbo.UN_AgencyUser b   ON b.AccountID = a.AccountID INNER JOIN CaileGame.dbo.CT_UsersPayDetail c  ON c.UserID = b.UserID WHERE a.AccountStatus = 1 and c.result = 1  ");
            strSql.Append("");
            if (!string.IsNullOrEmpty(StartTime))
            {
                strSql.Append(" and CompleteTime between @StartTime ");
                strTotalSql.Append(" and CompleteTime between @StartTime ");
                parms.Add("@StartTime", Convert.ToDateTime(StartTime));
            }
            if (!string.IsNullOrEmpty(EndTime))
            {
                strSql.Append(" and   @EndTime  ");
                strTotalSql.Append(" and   @EndTime  ");
                parms.Add("@EndTime", Convert.ToDateTime(EndTime));
            }
            RecordCount = base.GetIntSingle(string.Format("SELECT  count(1) FROM dbo.UN_Account a  INNER JOIN dbo.UN_AgencyUser b  ON b.AccountID = a.AccountID INNER JOIN CaileGame.dbo.CT_UsersPayDetail c  ON c.UserID = b.UserID WHERE a.AccountStatus = 1 and c.result = 1 {0}", strSql), parms);
            return new DataRepositoryBase<udv_PopularizeDetail>(DbConnectionEnum.CaileGame).GetListPaged(PageIndex, PageSize, strSql.ToString(), "CompleteTime desc", parms).ToList();
        }

        /// <summary>
        /// 绑定平台用户到代理帐号
        /// </summary>
        /// <param name="agentCode"></param>
        /// <param name="userID"></param>
        public void BindAgentAndUserDAL(long accountID, string userID)
        {
            var Parms = new DynamicParameters();
            const int Effect = 1;  //有效推广
            Parms.Add("@AccountID", accountID);
            string sql = "SELECT AccountStatus FROM [dbo].[UN_Account] WHERE AccountID = @AccountID;";
            var Entitys = new DataRepositoryBase<udv_Account>(DbConnectionEnum.CaileBusiness).QueryList(sql, Parms).ToList();
            if(null != Entitys && 0 < Entitys.Count)
            {
                if (Effect == Entitys[0].AccountStatus)
                {
                    Parms = new DynamicParameters();
                    sql = "INSERT [dbo].[UN_AgencyUser] ( AccountID, UserID, StartTime,  EndTime ) VALUES  ( @AccountID, @userID ,  @StartTime ,   @EndTime   )";
                    var agent = new AgencyUserEntity();
                    agent.AccountID = (int)accountID;
                    agent.UserID = int.Parse(userID);
                    agent.StartTime = DateTime.Now;
                    agent.EndTime = DateTime.Now.AddYears(1);
                    new DataRepositoryBase<AgencyUserEntity>(DbConnectionEnum.CaileBusiness).Insert(agent);
                }
            }
        }

    }
}
